﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MapCenterSystem.DataInfo;
using MapCenterSystem.DefineType;
using MapCenterSytem.Process;
using System.Drawing;
using System.Globalization;

namespace MobiMap_Service.Models
{
    public class DirectionControl
    {

        public static Direction BindListGraphNodeEx(Coordinate node1, Coordinate node2)
        {
            Direction directionMap = new Direction();
            string direction = string.Empty;
            string turnright = "Rẽ phải ";
            string turnleft = "Rẽ trái ";
            string gostraight = "Đi thẳng ";
            string distanceLanguage = string.Empty;
            List<GraphNodeInfoEx> lstGraphNode = GraphNode.ShortestPath(node1, node2);
            double angle;
            PointF A, B, C;
            string orientation = gostraight;
            string streetName = String.Empty;

            streetName = StreetEx.GetFullNameById(lstGraphNode[1].IDStreet);
            directionMap.LstGraphNode.Add(orientation + streetName);


            double distance = Coordinate.Distance(lstGraphNode[0].Location, lstGraphNode[1].Location);
            for (int i = 1; i < lstGraphNode.Count - 1; i++)
            {
                distance += Coordinate.Distance(lstGraphNode[i].Location, lstGraphNode[i + 1].Location);
                if (lstGraphNode[i].IDStreet == lstGraphNode[i + 1].IDStreet)
                {
                    continue;
                }
                else
                {
                    A = new PointF((float)lstGraphNode[i - 1].Location.Longitude,
                                            (float)lstGraphNode[i - 1].Location.Latitude);
                    B = new PointF((float)lstGraphNode[i].Location.Longitude,
                                            (float)lstGraphNode[i].Location.Latitude);
                    C = new PointF((float)lstGraphNode[i + 1].Location.Longitude,
                                            (float)lstGraphNode[i + 1].Location.Latitude);

                    System.Windows.Vector AB = new System.Windows.Vector(B.X - A.X, B.Y - A.Y);
                    System.Windows.Vector BC = new System.Windows.Vector(C.X - B.X, C.Y - B.Y);
                    angle = System.Windows.Vector.AngleBetween(AB, BC);


                    if (angle < 0)
                        orientation = turnright;
                    else
                        orientation = turnleft;
                }

                streetName = StreetEx.GetFullNameById(lstGraphNode[i + 1].IDStreet);
                directionMap.LstGraphNode.Add(orientation + streetName);
                
            }
            directionMap.Distance = distanceLanguage + ": " + distance.ToString("0.000", CultureInfo.InstalledUICulture) + " km";
            return directionMap;
        }
        //private void AddDirection(string graphNode, string orientation, string streetName)
        //{
        //    string str = orientation + streetName;
        //    graphNode = str;
        //}
    }
}